package com.fsnbigdata.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

/**
 * http://springfox.github.io/springfox/docs/current/
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket userSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("0.用户登陆")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/auth.*"),
                        regex( "/api/user/.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("用户登陆").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket organismSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("1.物种信息库")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                    regex("/api/organisms.*"),
                    regex( "/api/hint/.*"),
                    regex("/api/images.*"),
                    regex( "/api/videos.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("物种信息库").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket specimenSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("2.标本库")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/specimens.*"),
                        regex("/api/images.*"),
                        regex( "/api/videos.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("标本库").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket parkSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("3.动植物园库")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/parks.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("动植物园库").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket ecosystemSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("4.生态环境库")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/ecosystems.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("生态环境库").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket endangeredspeciesSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("5.生物资源保育与珍惜濒危库")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/endangeredspecies.*"),
                        regex("/api/plantconservations.*"),
                        regex("/api/animalconservations.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("生物资源保育与珍惜濒危库").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket vegetationsSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("6.植被库")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/vegetations.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("植被库").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket productsSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("7.生物产品与环境数据库")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/products.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("生物产品与环境数据库").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket enterprisesSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("8.生物企业及企业家信息数据库")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/enterprises.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("生物企业及企业家信息数据库").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket statisticSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("9.统计")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/statistic.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("统计").build())
                .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket documentsSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("10.文档")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/documents.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("文档").build())
                .securitySchemes(newArrayList(apiKey()));
    }
    
    @Bean
    public Docket zhkopuSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("11.生物科普首页")
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(or(
                regex("/api/zhkopu.*")))
            .build()
            .apiInfo(new ApiInfoBuilder().title("生物科普首页").build())
            .securitySchemes(newArrayList(apiKey()));
    }

    @Bean
    public Docket zhEcologyNavSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("12.旅游导航")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(or(
                        regex("/api/zhEcologyNav.*")))
                .build()
                .apiInfo(new ApiInfoBuilder().title("旅游导航").build())
                .securitySchemes(newArrayList(apiKey()));
    }
    
    @Bean
    public Docket zhkoppSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("13.生物科普")
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(or(
                regex("/api/zhkopp.*")))
            .build()
            .apiInfo(new ApiInfoBuilder().title("生物科普").build())
            .securitySchemes(newArrayList(apiKey()));
    }
    
    @Bean
    public Docket diversityreportSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("14.自动化报告")
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(or(
                regex("/api/diversityreport.*")))
            .build()
            .apiInfo(new ApiInfoBuilder().title("自动化报告").build())
            .securitySchemes(newArrayList(apiKey()));
    }
    
    @Bean
    public Docket zhHelpSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("15.帮助")
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(or(
                regex("/api/zhhelp.*")))
            .build()
            .apiInfo(new ApiInfoBuilder().title("帮助").build())
            .securitySchemes(newArrayList(apiKey()));
    }
    
    @Bean
    public Docket zhExpertSwagger() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("16.专家咨询")
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(or(
                regex("/api/zhexpert.*")))
            .build()
            .apiInfo(new ApiInfoBuilder().title("专家咨询").build())
            .securitySchemes(newArrayList(apiKey()));
    }

    private ApiKey apiKey() {
        return new ApiKey("Token", "Authorization", "header");
    }

    private ArrayList newArrayList(Object obj){
        ArrayList list = new ArrayList();
        list.add(obj);
        return list;
    }
}
